package com.yg.user.controller;


import com.yg.user.pojo.User;
import com.yg.user.pojo.vo.UserVo;
import com.yg.user.service.UserService;
import com.yg.util.JwtUtils;
import com.yg.util.Result;
import com.yg.util.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 魏成龙
 * @since 2020-11-07
 */
@RestController
@RequestMapping("/user/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public Result login(@RequestBody UserVo userVo) {
        Result result = userService.login(userVo);
        return result;
    }

    @GetMapping("getUserInfo")
    public Result getUserInfo(String token) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String userId = JwtUtils.getUserIdByToken(token);
        //查询数据库根据用户id获取用户信息
        User user = userService.getById(userId);
        return new Result(true, StatusCode.OK,"查询成功",user);
    }
}
